import openpyxl
import os


current_path = os.getcwd()  # 获取当前工作目录
parent_path = os.path.dirname(current_path)  # 获取上级目录
# 打开Excel文件

excel_name = parent_path + '/原始数据/当天生日的车主_20231215134808.xlsx'

workbook = openpyxl.load_workbook(excel_name)

# 获取工作表
sheet = workbook.active

# 获取推送模版文件(当天生日的车主-关注服务号.xlsm)
push_excel_name = '当天生日的车主-关注服务号.xlsm'
push_workbook = openpyxl.load_workbook(push_excel_name)
push_sheet = push_workbook.active

# 获取分割文件(当天生日的车主_关注公众号_分割文件_1.xlsx)
cut_excel_name = '当天生日的车主_关注公众号_分割文件_1.xlsx'
cut_workbook = openpyxl.load_workbook(cut_excel_name)
cut_sheet = cut_workbook.active

# 遍历指定列的所有单元格，获取Open ID数据（使用新的变量名）
for row in sheet.iter_rows():
    # 获取是否关注PWSA这一列的值
    if row[3].value == '关注':
        # 输出该行的第一列数据
        openid = [cell.value for cell in row][1]
        # print(openid)
        # 将openid数据插入到推送模版文件中
        push_sheet.append([openid])
        openid_cut = "\"" + openid + "\","
        # print(openid_cut)
        cut_sheet.append([openid_cut])

# 保存推送的Excel文件
push_workbook.save(push_excel_name)
cut_workbook.save(cut_excel_name)